﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace PowerArrow.nasa.View
{
    public class PopPanel
    {
        private bool _isShowing;

        private Popup _popup;

        private Grid _grid;

        private Canvas _canvas;

        public string Txt;
        public bool IsShowBtn = true;

        private FrameworkElement _content;
        protected FrameworkElement GetContent()
        {
            MesageBoard mb = new MesageBoard();
            mb.xTxt.Text = Txt;
            mb.xBtn.MouseLeftButtonUp += new MouseButtonEventHandler(xBtn_MouseLeftButtonUp);
            if (!IsShowBtn) {
                mb.xBtn.Visibility = Visibility.Collapsed;
            }
            return mb;
        }

        void xBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        protected void OnClickOutside() { }

        public void Show(PopPanelStyle style)
        {

            if (_isShowing)

                throw new InvalidOperationException();



            _isShowing = true;



            EnsurePopup(style);



            _popup.IsOpen = true;



            Application.Current.Host.Content.Resized += OnPluginSizeChanged;

        }

        public void Close()
        {

            _isShowing = false;



            if (_popup != null)
            {

                _popup.IsOpen = false;

                Application.Current.Host.Content.Resized -= OnPluginSizeChanged;

            }

        }
        private void EnsurePopup(PopPanelStyle style)
        {

            if (_popup != null)

                return;



            _popup = new Popup();

            _grid = new Grid();

            _popup.Child = _grid;



            if (style != PopPanelStyle.NonModal)
            {

                // If Canvas.Background != null, you cannot click through it



                _canvas = new Canvas();

                _canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };



                if (style == PopPanelStyle.Modal)
                {

                    _canvas.Background = new SolidColorBrush(Colors.Transparent);

                }

                else if (style == PopPanelStyle.ModalDimmed)
                {

                    _canvas.Background = new SolidColorBrush(Color.FromArgb(0x80, 0x80, 0x80, 0x80));

                }



                _grid.Children.Add(_canvas);

            }



            _grid.Children.Add(_content = GetContent());



            UpdateSize();

        }
        private void OnPluginSizeChanged(object sender, EventArgs e)
        {

            UpdateSize();

        }



        private void UpdateSize()
        {

            //_grid.Width = Application.Current.Host.Content.ActualWidth;

            //_grid.Height = Application.Current.Host.Content.ActualHeight;

            _grid.Width = 460;

            _grid.Height = 360;

            if (_canvas != null)
            {

                _canvas.Width = _grid.Width;

                _canvas.Height = _grid.Height;

            }

        }
    }
    public enum PopPanelStyle
    {

        NonModal,

        Modal,

        ModalDimmed
    }
}
